package com.tzbank.wisdom.service.impl;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.DateUtils;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.utils.String2Utils;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.dao.LoanMapper;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.service.LoanService;
import com.tzbank.wisdom.utils.BusinessCodeUtil;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Service(value = "loanService")
@Slf4j
@SuppressWarnings({ "rawtypes", "unchecked" })
public class LoanServiceImpl implements LoanService {

	@Autowired
	private LoanMapper loanMapper;
	
	@Resource
	private CommonService commonService;

	/**
	 * @param 获取公共参数
	 * @return
	 */
	public Map<String, Object> getInputMap(Map<String, Object> body) {

		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap = RequestMap.getInputMap(body);
		Map<String, Object> usermap = RequestMap.getInputUserInfo((String) body.get("clientSeqNo"));
		if (usermap.get("clientNo") == null) {
			return usermap;
		}
		inputMap.putAll(usermap);
		return inputMap;
	}

	/**
	 * 贷款-贷款申请进度查询 
	 * 贷款申请信息查询(02003000040) 
	 * 贷款申请进度查询(04) 客户贷款申请相关信息查询
	 * 根据客户号查询该客户名下贷款业务的申请进度。
	 * 
	 */
	public Map<String, Object> getLoanScheduleInfos(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_SCHE_SEARCH.getServiceCode(),
				TransCodeEnum.LOAN_SCHE_SEARCH.getServiceSence(), "", SysConstans.ABP_CODE, "304010");// 调用系统代码 核心是301010 信贷是304010
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(body);
		// 调用核心计价因子报文头入参
		inputMap.put("sysHead", reqSysHead);
		// 报文体
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("queryResultArray");
			
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> params = (Map<String, Object>) listArray.get(i);
				String signTimestamp = (String) params.get("signTimestamp");
				String applyTimestamp = signTimestamp.substring(0, 10);
				realDate.put("approveStatus", (String) params.get("approveStatus"));// 审批状态
				realDate.put("signTimestamp", signTimestamp);// 申请时间 yyyy-DD-mm hh:mm:ss
				realDate.put("applyTimestamp", applyTimestamp);// 申请时间 yyyy-DD-mm
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("queryResultArray");
			data.put("LoanResultArray", realList);
			resultMap.remove("data");
			resultMap.put("data", data);
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款-快易贷列表查询 
	 * 贷款合同信息查询(02003000026)
	 *  快易贷合同信息查询(07) 
	 *  贷款合同相关信息查询
	 * 客户通过我行个人网银系统查询其有效的快易贷和随易贷合同信息。
	 * clientNo ： 1045745178 有数据
	 */
	public Map<String, Object> getQuickLoanList(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_QUICK_LIST.getServiceCode(),
				TransCodeEnum.LOAN_QUICK_LIST.getServiceSence(), "", SysConstans.ABP_CODE, "304010");// 调用系统代码 核心是301010  信贷是304010
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(body);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("bussType", "010002");// 快易贷业务类型 -----010002
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("queryResultArray");
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> params = (Map<String, Object>) listArray.get(i);
				String startDate = (String) params.get("startDate");
				String _startDate = DateUtils.getdatefor_yyyy_MM_dd(startDate);
				String matureDate = (String) params.get("matureDate");
				String _matureDate = DateUtils.getdatefor_yyyy_MM_dd(matureDate);
				realDate.put("contractNo", (String) params.get("contractNo"));// 合同号
				realDate.put("startDate", _startDate);// 开始日期
				realDate.put("matureDate", _matureDate);// 到期日期
				realDate.put("amt", (double) params.get("amt"));// 合同金额
				realDate.put("contracBalance", (double) params.get("contracBalance"));// 合同余额
				realDate.put("monRate", (double) params.get("monRate"));// 月利率
				realDate.put("settleAcctNo", (String) params.get("settleAcctNo"));// 结算账号
				realDate.put("productCode", (String) params.get("productCode")); // 产品编号
				inputMap.putAll(params);
				Map<String, Object> intest = getLoanInterestRateInfosQry(inputMap);
				realDate.putAll((Map<? extends String, ? extends Object>) intest.get("data"));
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("queryResultArray");
			data.put("LoanResultArray", realList);
			resultMap.remove("data");
			resultMap.put("data", data);
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 *贷款发放存贷积数比
	 *贷款利率信息查询(02003000045) 
	 *贷款利率试算列表查询(02) 
	 *查询贷款利息信息
	 *查询贷款积分比、利率列表
	 * 
	 */
	public Map<String, Object> getLoanInterestRateInfosQry(Map<String, Object> body) {
		// String clientNo = (String)body.get("clientNo");
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				TransCodeEnum.LOAN_INTEREST_RATE_INFOS.getServiceCode(),
				TransCodeEnum.LOAN_INTEREST_RATE_INFOS.getServiceSence(), "", SysConstans.ABP_CODE, "108060");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("productCode", body.get("productCode")); // 产品编号Y P01001000200006
		inputMap.put("contractNo", body.get("contractNo"));// 合同号
		inputMap.put("clientNo", body.get("clientNo"));// 客户号Y 1411643386
		inputMap.put("interestAmt", body.get("amt"));// 计息金额Y 500000.00
		inputMap.put("startDate", body.get("startDate"));// 开始日期Y 20530506
		inputMap.put("matureDate", body.get("matureDate"));// 到期日期Y 20540506
		inputMap.put("ccy", "CNY");// 币种Y CNY
		inputMap.put("bussType", "A3");// 业务类型Y A4
		inputMap.put("orgId", "0117");// 所属机构Y 0117
		inputMap.put("totalLoanTimes", "0");// 累计贷款次数 0
		inputMap.put("guaranteeMode", "201");// 担保方式Y 201
		inputMap.put("accrualScaleLevel", "1");// 积数比级别Y 1
		inputMap.put("plnRsetFlag", "0");// 是否可协商 0
		inputMap.put("intRateConvUnit", "");// 年利率转换单位 年利率转换单位 D-日利率 M-月利率 Y-年利率 默认为M-月利率
		inputMap.put("duebillNo", "");// 借据号
		inputMap.put("intDate", "");// 利率日期
		inputMap.put("dicValue", "");// 特定维度值
		inputMap.put("SpecialProductCode", "");// 特殊产品编号
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List intestArray = (List) data.get("intestArray");
			List<Object> realList = new ArrayList<Object>();
			Map<String, Object> realDate = new HashMap<String, Object>();
			Map<String, Object> params = (Map<String, Object>) intestArray.get(intestArray.size() - 1); // 获取最后一条利率信息
			realDate.put("intRate", (double) params.get("intRate"));
			realDate.put("accrualScaleNo", params.get("accrualScaleNo"));
			realList.add(realDate);
			data.remove("head");
			data.remove("intestArray");
			resultMap.remove("data");
			resultMap.put("data", realDate);
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;

	}

	/**
	 * 贷款合同信息查询 
	 * 贷款合同信息查询(02003000026) 
	 * 贷款合同信息查询_非柜(13) 
	 * 贷款合同相关信息查询
	 * 根据客户号、卡号、业务类型等信息查询自助贷款合同信息，用于非柜面发起的交易。
	 */
	public Map<String, Object> getLoanContractMsgQry(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_CONTRACT_MSG.getServiceCode(),
				TransCodeEnum.LOAN_CONTRACT_MSG.getServiceSence(), "", SysConstans.ABP_CODE, "");// 调用系统代码 核心是301010 信贷是304010
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("clientNo", body.get("clientNo"));// Y客户号 ----5300108213
		inputMap.put("queryType", "KHDKHTCX"); // Y操作类型:-----KHDKHTCX
		inputMap.put("acctNo", "");// 账号
		inputMap.put("contractNo", "");// 合同号
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("listArray");		
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> params = (Map<String, Object>) listArray.get(i);
				String loanDateStart = (String) params.get("loanDateStart");
				String _loanDateStart = DateUtils.getdatefor_yyyy_MM_dd(loanDateStart);
				String loanDateEnd = (String) params.get("loanDateEnd");
				String _loanDateEnd = DateUtils.getdatefor_yyyy_MM_dd(loanDateEnd);
				realDate.put("contractNo", (String) params.get("contractNo"));
				realDate.put("productCode", (String) params.get("productCode"));
				realDate.put("productName", (String) params.get("productName"));
				realDate.put("loanDateStart", _loanDateStart);
				realDate.put("loanDateEnd", _loanDateEnd);
				realDate.put("loanAmt", (double) params.get("loanAmt"));
				realDate.put("contracBalance", (double) params.get("contracBalance"));
				realDate.put("repayType",   (String) params.get("repayType"));
				realDate.put("firstDisFlag", (String) params.get("firstDisFlag"));
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("listArray");
			data.put("LoanResultArray", realList);
			resultMap.remove("data");
			resultMap.put("data", data);
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		}  else if("4444".equals(resultMap.get("code"))){
			
			return ResultUtil.resultMap("无此自助贷款合同信息", "4444",
					resultMap.get("msg"));
	
		}else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款未还清记录查询 贷款借据信息查询(02003000007) 客户借据信息查询_非柜(16) 实现贷款借据基本信息查询功能
	 * 根据客户号，返回客户号下指定类型的所有借据信息借据信息（除贴现、外币贷款且有余额） 有数据 llz20180615
	 */
	public Map<String, Object> getLoanDebitMsgQry(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_DETBIL_MSG.getServiceCode(),
				TransCodeEnum.LOAN_DETBIL_MSG.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("queryType", "KHJJXXCX"); // 操作类型y------KHJJXXCX
		inputMap.put("clientNo", body.get("clientNo"));// 客户号-----1403400002
		inputMap.put("acctNo", "");// 账号
		inputMap.put("duebillNo", "");// 借据号
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("listArray");
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> kyd = (Map<String, Object>) listArray.get(i);
				String grantDate = (String) kyd.get("grantDate");
				String _grantDate = DateUtils.getdatefor_yyyy_MM_dd(grantDate);
				String matureDate = (String) kyd.get("matureDate");
				String _matureDate = DateUtils.getdatefor_yyyy_MM_dd(matureDate);
				// 造假数据
				if (i == 2 || i == 3) {
					realDate.put("productCode", "P01001000200001");
				} else if (i == 4 || i == 5) {
					realDate.put("productCode", "P11001000100001");
				} else {
					realDate.put("productCode", (String) kyd.get("productCode"));
				}
				realDate.put("loanBalance", (double) kyd.get("loanBalance"));
				realDate.put("productName", (String) kyd.get("productName"));
				realDate.put("duebillNo", (String) kyd.get("duebillNo"));
				realDate.put("duebillType", (String) kyd.get("duebillType"));
				realDate.put("grantDate", _grantDate);
				realDate.put("matureDate", _matureDate);
				realDate.put("grantLmt", (double) kyd.get("grantLmt"));			
				realDate.put("retTotalAmt", (double) kyd.get("retTotalAmt"));
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("listArray");
			data.put("LoanResultArray", realList);
			resultMap.remove("data");
			resultMap.put("data", data);
		} else if("4444".equals(resultMap.get("code"))){
			
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), "4444",
					resultMap.get("msg"));
	
		} else{
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款还款计划列表查询 MCLoanRepayPlanDetailQry 
	 * 贷款还款明细查询(02003000011) 
	 * 贷款未还清明细查询_非柜面(15)
	 * 贷款还款相关信息查询 
	 */
	public Map<String, Object> getLoanRepayPlanDetailQry(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_REPAY_PLAN_DETAIL.getServiceCode(),
				TransCodeEnum.LOAN_REPAY_PLAN_DETAIL.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("clientNo", body.get("clientNo"));// 客户号1300107966
		inputMap.put("sysHead", reqSysHead);
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("listArray");
			List<Object> realList = new ArrayList<Object>();
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> kyd = (Map<String, Object>) listArray.get(i);
				String grantDate = (String) kyd.get("grantDate");
				String _grantDate = DateUtils.getdatefor_yyyy_MM_dd(grantDate);
				String matureDate = (String) kyd.get("matureDate");
				String _matureDate = DateUtils.getdatefor_yyyy_MM_dd(matureDate);
				realDate.put("retTotalAmt", (double) kyd.get("retTotalAmt"));
				realDate.put("duebillNo", (String) kyd.get("duebillNo"));
				realDate.put("ctrlAcct", (String) kyd.get("ctrlAcct"));
				realDate.put("grantDate", _grantDate);
				realDate.put("matureDate", _matureDate);
				realDate.put("notTotalAmt", (double) kyd.get("notTotalAmt"));
				realList.add(realDate);
			}
			data.remove("head");
			data.remove("listArray");
			data.put("LoanResultArray", realList);
			resultMap.remove("data");
			resultMap.put("data", data);
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}
	/**
	 * 贷款到期日查询 
	 * 贷款到期信息查询(02003000039) 
	 * 贷款到期日查询(02) 
	 * 查询贷款到期信息，如贷款还款到期日、贷款临时提额到期等等
	 * 业务枢纽场景，非柜面使用，实现贷款到期日期计算功能
	 */
	public Map<String, Object> getLoanDueDateQry(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_DUE_DATE.getServiceCode(),
				TransCodeEnum.LOAN_DUE_DATE.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("operationType", "DKDQRJS"); // Y操作类型---DKDQRJS
		inputMap.put("contractNo", body.get("contractNo"));// Y合同号 ----0117002634
		inputMap.put("productCode", body.get("productCode"));// 产品编号 ----P01001000400001
		inputMap.put("matureDate", "");// 到期日期
		inputMap.put("period", "");// 期数(周期)
		inputMap.put("repayType", "");// 还款方式
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequestNoHead(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款利率计算 
	 * 贷款计算器(02002000005) 
	 * 贷款利率计算(02) 
	 * 贷款的利率及每期还款金额等的计算。 
	 * 贷款利率计算
	 */
	public Map<String, Object> getLoanInterestRateCalculation(Map<String, Object> body) {
		// String clientNo = (String)body.get("clientNo");
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				TransCodeEnum.LOAN_INTEREST_RATE_CALCULATION.getServiceCode(),
				TransCodeEnum.LOAN_INTEREST_RATE_CALCULATION.getServiceSence(), "", SysConstans.ABP_CODE, "108060");// 调用系统代码  核心是301010   信贷是304010
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("operationType", body.get("operationType")); // 操作类型y ------ 2
		inputMap.put("productCode", body.get("productCode")); // 产品编号Y------P01001000100001
		inputMap.put("duebillNo", body.get("duebillNo"));// 借据号-----jj0117099811
		inputMap.put("clientNo", body.get("clientNo"));// 客户号Y-------1002610002
		inputMap.put("ccy", body.get("ccy"));// 币种Y CNY
		inputMap.put("bussType", body.get("bussType"));// 业务类型Y A3
		inputMap.put("orgId", body.get("orgId"));// 所属机构 0117
		inputMap.put("intRateConvUnit", body.get("intRateConvUnit"));// 年利率转换单位 Y
		inputMap.put("interestAmt", body.get("interestAmt"));// 计息金额Y 50000.00
		inputMap.put("startDate", body.get("startDate"));// 开始日期 20530506
		inputMap.put("matureDate", body.get("matureDate"));// 到期日期 20540506
		inputMap.put("totalLoanTimes", body.get("totalLoanTimes"));// 累计贷款次数 4
		inputMap.put("guaranteeMode", body.get("guaranteeMode"));// 担保方式101
		inputMap.put("accrualScaleNo", body.get("accrualScaleNo"));// 积数比级别 101
		inputMap.put("plnRsetFlag", body.get("plnRsetFlag"));// 是否可协商 1
		inputMap.put("contractRate", body.get("contractRate"));// 合同利率 11.52
		inputMap.put("contractNo", "");// 合同号
		inputMap.put("intDate", "");// 利率日期
		inputMap.put("instlPeriod", "");// 分期期数
		inputMap.put("dicValue", "");// 特定维度值
		inputMap.put("SpecialProductCode", "");// 特殊产品编号
		inputMap.put("loanAccrual", null);// 贷款积数
		inputMap.put("maxAccrualScaleN", "");// 最高积数比例编号
		inputMap.put("minAccrualScaleN", "");// 最低积数比例编号
		inputMap.put("addAccrual", null);// 客户附加积数
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 小本贷款逾期还款利息查询 
	 * 贷款还款明细查询(02003000011) 
	 * 贷款还款分期类信息查询(23) 
	 * 贷款还款相关信息查询
	 * "其他渠道上送借据号、指定期数到核心，核心返回该期未归还本金、未归还利息和与逾期利息 适用还款方式为0002分期类贷款查询。 适用于非利率后定价情况"
	 */
	public Map<String, Object> getLoanRepaymentInterestQry(Map<String, Object> body) {
		// String clientNo = (String)body.get("clientNo");
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				TransCodeEnum.LOAN_REPAY_MENT_INTEREST.getServiceCode(),
				TransCodeEnum.LOAN_REPAY_MENT_INTEREST.getServiceSence(), "", SysConstans.ABP_CODE, "");// 调用系统代码 核心是301010 信贷是304010
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("operationType", "DKHKFQCX"); // 操作类型y====DKHKFQCX
		inputMap.put("duebillNo", body.get("duebillNo"));// 借据号=====jj0117099299
		inputMap.put("overPeriod", "");// 逾期期数
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款利息计算 
	 * 贷款计算器(02002000005) 
	 * 综合贷款利息计算(06) 
	 * 贷款的利率及每期还款金额等的计算。
	 * 渠道系统调用BIP系统，由BIP系统先调用核心获取是否重定价标识，如果是，则调用计价平台获取贷款利率，再根据贷款利率去核心计算利息，如果不是重定价，直接根据合同利率去核心计算利息
	 */
	public Map<String, Object> getLoanInterestCalulation(Map<String, Object> body) {
		// String clientNo = (String)body.get("clientNo");
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				TransCodeEnum.LOAN_INTEREST_CALCULATION.getServiceCode(),
				TransCodeEnum.LOAN_INTEREST_CALCULATION.getServiceSence(), "", SysConstans.ABP_CODE, "108060");// 调用系统代码 核心是301010 信贷是304010
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("productCode", body.get("productCode")); // 产品编号----P04001000700001
		inputMap.put("duebillNo", body.get("duebillNo"));// 借据号----jj0117099811
		inputMap.put("calculateType", body.get("calculateType"));// 计算类型----2
		inputMap.put("tranType", body.get("tranType"));// 交易类型-----2
		inputMap.put("contractNo", "");// 合同号
		inputMap.put("insltPeriod", "");// 分期期数
		inputMap.put("repayAmt", "");// 还款金额
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款申请(02001000016)
	 *  贷款申请(02) 
	 *  贷款业务申请 
	 *  通过智脑平台贷款申请(24小时营业厅业务)
	 */
	public Map<String, Object> loanApply(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_APPLY.getServiceCode(),
				TransCodeEnum.LOAN_APPLY.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap = RequestMap.getInputMap(body);
		Map<String, Object> usermap = RequestMap.getInputUserInfo((String) body.get("clientSeqNo"));
		if (usermap.get("clientNo") == null) {
			return usermap;
		}
		inputMap.putAll(usermap);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("taskId", "5A54673"); // 任务编号 Y
		inputMap.put("globalType", "I");// 证件类型 Y
		inputMap.put("globalId", body.get("globalId"));// 证件号码 Y
		inputMap.put("clientName", body.get("clientName"));// 客户名称 Y
		inputMap.put("mobileNo", body.get("mobileNo")); // 移动电话 Y
		inputMap.put("applyAmt", body.get("applyAmt"));// 申请金额 Y
		inputMap.put("applyerType", body.get("applyerType"));// 申请人属性 Y
		inputMap.put("subBranchId", "153"); // 网点号(分行行号) Y
		inputMap.put("usage", body.get("usage"));// 用途
		inputMap.put("manageItem", body.get("manageItem"));// 经营项目 Y
		inputMap.put("referrerId", "");// 推荐人工号
		inputMap.put("remark", ""); // 备注(摘要)
		inputMap.put("subBranchAdd", "");// 营业网点地址
		inputMap.put("orgAreaName", "");// 所属区域名称
		inputMap.put("address", "");// 地址
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;
	}

	/**
	 * 贷款放款前置页面信息查询
	 */
	public Map<String, Object> loanPayment(Map<String, Object> body) {
	
		Map<String, Object> loanMap = new HashMap<String, Object>();
					String productCode = (String) body.get("productCode");
					//获取产品功能，是否具有放款功能
							if(productCode == null || productCode.isEmpty()){				
								log.info(">>>>>贷款信息查询错误，贷款产品编号为："+productCode+"<<<<<<=");
							}else{
										List<Map> list	= 	loanMapper.qryLoanShelfPrdInfo( productCode);
										//获取产品功能，是否具有放款功能
										 for(int i=0;i<list.size();i++ ) {
											 Map<String,String> map = (Map) list.get(i);	
												String loanFuncId = map.get("LOANFUNCID");							
												String[] funcIds = loanFuncId.split(",");
												for(int j = 0; j < funcIds.length; j++){
													String funcId = funcIds[j];
													if("FUNC000002".equals(funcId)){//功能ID
														loanMap.put("IsPament", "Y");//允许放款
													}
												}
										 }							 
										 //查询到期日来源(组件信息查询)											
											Map qry = new HashMap();//查询条件
											qry.put("productCode", productCode);
											qry.put("LoanPrdFieldCode", "PCA0000036");
											qry.put("BankId", "04590501");									
										List<Map> date_list =  loanMapper.qryLoanPrdComInfo(qry);					
										 if(date_list.size()==0) {
												loanMap.put("IsEndDate", "Y");//渠道输入
										 }else {
											 for(int i=0;i<date_list.size();i++ ) {
												 Map<String,String> map = (Map) date_list.get(i);	
													String rangeValue =map.get("LOANPRDRANGEVALUE");
													if(!StringUtils.isEmpty(rangeValue)) {
														String[] ranges = rangeValue.split(",");
														for(int j = 0; j < ranges.length; j++){
															String range = ranges[j];
															if("1".equals(range)){
																loanMap.put("IsEndDate", "Y");//渠道输入
																break;
															}else{
																//核心查询到期日期
																Map qryMap = new HashMap();//查询条件
																qryMap.put("productCode", "P01001000400001");
																qryMap.put("contractNo", "0117002634");// Y合同号 ----0117002634							
																Map<String, Object> resultMap =getLoanDueDateQry(qryMap);										
																Map<String, String> result=	(Map<String, String>) resultMap.get("data");
																String NextDate = result.get("matureDate");
																loanMap.put("NextDate", NextDate);//到期日期*/
															}
														}													
													}else {
														loanMap.put("IsEndDate", "Y");//渠道输入
													}
											 }	
										 }					 
										   //查询是否使用合同利率					
											Map qry_htll = new HashMap();//查询条件
											qry_htll.put("productCode", productCode);
											qry_htll.put("LoanPrdFieldCode", "PCA0000047");
											qry_htll.put("BankId", "04590501");												
											List<Map> htll_list =loanMapper.qryLoanPrdComInfo(qry_htll);
											 for(int i=0;i<htll_list.size();i++ ) {
												 Map<String,String> map = (Map) date_list.get(i);	
													String rangeValue =map.get("LOANPRDRANGEVALUE");
													if(!StringUtils.isEmpty(rangeValue)) {
														String[] ranges = rangeValue.split(",");
														for(int j = 0; j < ranges.length; j++){
															String range = ranges[j];
															if("0".equals(range)){//不使用合同利率
																loanMap.put("IsRete", "Y");//查询积数比
															}													
														}															
													}																			
											 }	
				}
					return loanMap;				
		/*Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();
		reqSysHead.put(SysConstans.SYS_TRAN_CODE, "02003000007"); // 服务码
		reqSysHead.put(SysConstans.SYS_TRAN_SENCE, "16");
		reqSysHead.put("consumerId", SysConstans.ABP_CODE);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("contractNo", "0117006070"); // 合同号 y
		inputMap.put("payerAcNo", body.get("payerActNo"));// 账号
		inputMap.put("amount", body.get("amount"));// 发放金额
		inputMap.put("endDate", body.get("contractNo"));// 合同号
		inputMap.put("interestRate", body.get("duebillNo")); // 利率
		inputMap.put("ratioOfDepToLoan", body.get("loanType"));// 存贷积数比
		inputMap.put("oTPPassword", body.get("notTotalAmt"));// 动态口令号
		inputMap.put("period", body.get("returnInterest"));// 期数
		inputMap.put("interrstAcctNo", body.get("overInterest")); // 结算账号
		inputMap.put("payerAcName", body.get("repayAmt"));// 付款人户名
		inputMap.put("payeeAcName", body.get("isOverRetDate"));// 收款人户名
		inputMap.put("penalInterest", body.get("period"));// 逾期利率
		inputMap.put("payerDeptName", body.get("payType")); // 交易银行
		inputMap.put("contracBalance", body.get("payType")); // 合同余额
		inputMap.put("intRate", body.get("payType")); // 利率
*/		
	}

	/**
	 * 贷款还款
	 */
	public Map<String, Object> loanRepay(Map<String, Object> body) {
		// String clientNo = (String)body.get("clientNo");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();
		reqSysHead.put(SysConstans.SYS_TRAN_CODE, "02003000007"); // 服务码
		reqSysHead.put(SysConstans.SYS_TRAN_SENCE, "16");
		reqSysHead.put("consumerId", SysConstans.ABP_CODE);
		inputMap.put("sysHead", reqSysHead);

		inputMap.put("custId", body.get("custId")); // 客户号
		inputMap.put("payerActNo", body.get("payerActNo"));// 付款人账号
		inputMap.put("amount", body.get("amount"));// 还款本金金额
		inputMap.put("contractNo", body.get("contractNo"));// 合同号
		inputMap.put("duebillNo", body.get("duebillNo")); // 借据号
		inputMap.put("loanType", body.get("loanType"));// 贷款类型
		inputMap.put("notTotalAmt", body.get("notTotalAmt"));// 未还总额
		inputMap.put("returnInterest", body.get("returnInterest"));// 还款利息
		inputMap.put("overInterest", body.get("overInterest")); // 逾期利息
		inputMap.put("repayAmt", body.get("repayAmt"));// 还款总额
		inputMap.put("isOverRetDate", body.get("isOverRetDate"));// 是否逾期
		inputMap.put("period", body.get("period"));// 期数
		inputMap.put("payType", body.get("payType")); // 还款方式
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		return resultMap;
	}

	/**
	 * 贷款-快易贷贷款入账申请
	 *  贷款申请信息管理(02002000019) 
	 *  快易贷放款申请登记(05)
	 *   贷款申请信息登记、维护
	 * 快易贷放款申请登记(24小时营业厅业务)
	 */
	public Map<String, Object> fastLoanApply(Map<String, Object> body) {

		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOAN_FASTAPPLY.getServiceCode(),
				TransCodeEnum.LOAN_FASTAPPLY.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		// inputMap = RequestMap.getInputMap(body);

		inputMap.put("logonType", "R");
		inputMap.put("clientIp", "127.0.0.1");
		inputMap.put("bankSystemType", "04590501");

		Map<String, Object> usermap = RequestMap.getInputUserInfo("500000604");
		if (usermap.get("clientNo") == null) {
			return usermap;
		}
		inputMap.putAll(usermap);

		inputMap.put("sysHead", reqSysHead);
		inputMap.put("contractNo", "0117006070"); // 合同号 y
		inputMap.put("loanAmt", 10000.0);// 贷款金额
		inputMap.put("matureDate", "20190131");// 到期日期
		inputMap.put("accrualScaleNo", "101"); // 积数比例编号
		inputMap.put("productCode", "P01001000200001"); // 合同号 y
		inputMap.put("monRate", "8.67");// 月利率
		inputMap.put("loanPurpuse", "买衣服");// 贷款用途
		// inputMap.put("accrualScale", body.get("accrualScale"));//积数比例
		// inputMap.put("writeOffAccrual", body.get("writeOffAccrual"));// 冲减积数
		// inputMap.put("otpPassword", body.get("otpPassword"));// 动态密码
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		return null;
	}


	/**
	 * 发送贷款验证码
	 */

	public Map<String, Object> sendAuthCode(Map<String, String> inputMap) {
		inputMap.put("smsInfo", "您正在进行贷款申请交易");
		Map<String, Object> resultMap = commonService.sendMsgToMB(inputMap);
		if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			log.info("响应信息：[{}]", JSONObject.fromObject(resultMap).toString());
		} else {
			log.info("ESB报文数据返回出错！");
			return ResultUtil.resultMap(BusinessCodeUtil.CODE_40001.getName(), BusinessCodeUtil.CODE_40001.getCode(),
					resultMap.get("msg"));
		}
		return resultMap;

	}

	
	
	
	/***
	 * 我的贷款信息查询
	 */
	public Map<String, Object> getMyLoan(Map<String, Object> body) {	
		//授信总合度通过贷款合同列表接口查询，累加合同总金额
		Map<String, Object> creditMap = getLoanContractMsgQry(body);
		double creditLoanAll = 0.00;
		// 当ESB数据返回成功的时候进行数据处理。
				if (CodeUtil.CODE_200.getCode().equals(creditMap.get("code"))) {
					Map<String, Object> data = (Map<String, Object>) creditMap.get("data");
					List listArray = (List) data.get("LoanResultArray");					
					if(listArray.size()==0) {
						creditLoanAll=0.00;
					}else {
						for (int i = 0; i < listArray.size(); i++) {
							Map<String, Object> params = (Map<String, Object>) listArray.get(i);				
							creditLoanAll+=(double) params.get("loanAmt");
						}
						}						
					}  else if ("4444".equals(creditMap.get("code"))){
						//无此自助贷款合同信息
						creditLoanAll=0.00;
					}
			
			double borrowLoanAll = 0.00;
			//借款总金额通过未还请借据接口查询。累加应还金额
				body.put("clientNo", "1403400002");
			Map<String, Object> borrowMap = getLoanDebitMsgQry(body);
			// 当ESB数据返回成功的时候进行数据处理。
			if (CodeUtil.CODE_200.getCode().equals(borrowMap.get("code"))) {
				Map<String, Object> data = (Map<String, Object>) borrowMap.get("data");
				List listArray = (List) data.get("LoanResultArray");	
				for (int i = 0; i < listArray.size(); i++) {
					Map<String, Object> kyd = (Map<String, Object>) listArray.get(i);
					borrowLoanAll+=(double) kyd.get("retTotalAmt");
				}			
			} else if ("4444".equals(borrowMap.get("code"))){
				//客户没有未还清的贷款记录
				borrowLoanAll=0.00;
			}	
			
			
			//近期还款总额
			double repayLoanAll = 0.00;
			
			
			
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("creditLoanAll", creditLoanAll);
			resultMap.put("borrowLoanAll", borrowLoanAll);
			resultMap.put("repayLoanAll", repayLoanAll);
			resultMap.put("notuseLoanAll", creditLoanAll-borrowLoanAll);


			return ResultUtil.resultMap(ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(),
					JSONObject.fromObject(resultMap).toString());
	}
	
	
	
	
	
	
	public static void main(String[] args) throws ParseException {

		
		
	}
	
	/**
	 * 贷款---借款总金额
	 * @param body
	 * @return
	 */
	public Map<String, Object> getBorrowLoan(Map<String, Object> body) {
	
		double borrowLoanAll = 0.00;
		//借款总金额通过未还请借据接口查询。累加应还金额
			body.put("clientNo", "1403400002");
		Map<String, Object> borrowMap = getLoanDebitMsgQry(body);
		// 当ESB数据返回成功的时候进行数据处理。
		if (CodeUtil.CODE_200.getCode().equals(borrowMap.get("code"))) {
			Map<String, Object> data = (Map<String, Object>) borrowMap.get("data");
			List listArray = (List) data.get("LoanResultArray");	
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> kyd = (Map<String, Object>) listArray.get(i);
				borrowLoanAll+=(double) kyd.get("retTotalAmt");
			}			
		} else if ("4444".equals(borrowMap.get("code"))){
			//客户没有未还清的贷款记录
			borrowLoanAll=0.00;
		}			
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("borrowLoanAll", borrowLoanAll);
		return ResultUtil.resultMap(ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(),
				JSONObject.fromObject(resultMap).toString());
	}

	
	/**
	 * 贷款营销
	 * @param body
	 * @return
	 */
	public Map<String, Object> loanMarketing(Map<String, String> body) {
	
			
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
	//	loanMapper.s
		
		
		
		
		return ResultUtil.resultMap(ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(),
				JSONObject.fromObject(resultMap).toString());
	}
	

	

}
